#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "ds1338c.h"

void help()
{
	printf("\tparameter : [-y] year [-m] month [-d] day [-t] hour [-u] minute [-s] second [-h] help\n");
}

int main(int argc, char **argv)
{
	int dev, tmp;
	ds_rtc_time time;
	int ch;

	if (argc < 2) {
		help();
		exit(1);
	}
	
	while ((ch = getopt(argc, argv, "y:m:d:t:u:s:h")) != -1) {
		switch(ch) {
			case 'y':
				time.year = (atoi(optarg));
				break;
			case 'm':
				time.month = (atoi(optarg));
				break;
			case 'd':
				time.day = (atoi(optarg));
				break;
			case 't':
				time.hour = (atoi(optarg));
				break;
			case 'u':
				time.minute = (atoi(optarg));
				break;
			case 's':
				time.second = (atoi(optarg));
				break;
			case 'h':
				help();
				exit(1);
				break;
			case '?':
				printf("Unknown option : %c.\n", (char)optopt);
			default:
				break;
		}
	}

	printf("\t[application][%s:%d]\t[%d %d %d %d %d %d]\n",__FUNCTION__,
			__LINE__, time.year, time.month, time.day, time.hour, time.minute, time.second);


	#ifdef HI3511
	dev = open("/dev/misc/ds1338", O_RDONLY);
	#else
	dev = open("/dev/ds1338", O_RDONLY);
	#endif
	if (dev <= 0) {
		printf("ds1338 device open fail\n");
		exit(0);
	}

	/* set time */
	tmp = ioctl(dev, DS1338_RTC_WRITE_TIME,&time);

	close(dev);

	return 0;
}

// writetime-21 -y 13 -m 5 -d 15 -t 9 -u 38 -s 45
